package com.crayfish.quduo.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.widget.RadioGroup;

import com.crayfish.quduo.App;
import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseActivity;
import com.crayfish.quduo.ui.fragment.ClassfyFragment;
import com.crayfish.quduo.ui.fragment.HomeFragment;
import com.crayfish.quduo.ui.fragment.MeFragment;
import com.crayfish.quduo.utils.ToastUtil;

import butterknife.BindView;
import cn.bmob.v3.listener.BmobUpdateListener;
import cn.bmob.v3.update.BmobUpdateAgent;
import cn.bmob.v3.update.UpdateResponse;
import cn.bmob.v3.update.UpdateStatus;

public class MainActivity extends BaseActivity{

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    private HomeFragment homeFragment;
    private ClassfyFragment classfyFragment;
    private MeFragment meFragment;
    private FragmentTransaction transaction;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEventAndData() {
        initFragment();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio_home:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frame,getHomeFragment())
                                .commit();
                        break;
                    case R.id.radio_classify:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frame,getClassfyFragment())
                                .commit();
                        break;
                    case R.id.radio_me:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frame,getMeFragment())
                                .commit();
                        break;
                }
            }
        });
        BmobUpdateAgent.setUpdateListener(new BmobUpdateListener() {
            @Override
            public void onUpdateReturned(int i, UpdateResponse updateResponse) {
                if(i == UpdateStatus.Yes){

                }else if(i == UpdateStatus.IGNORED){
                    ToastUtil.show("该版本已被忽略更新");
                }else if(i == UpdateStatus.No){
                    ToastUtil.show("已是最新版本");
                }
            }
        });
    }

    private void initFragment(){
        setSupportActionBar(toolbar);
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.frame,getHomeFragment());
        transaction.commit();
    }

    private Fragment getHomeFragment(){
        if(homeFragment == null){
            homeFragment = new HomeFragment();
        }
        return homeFragment;
    }
    private Fragment getMeFragment(){
        if(meFragment == null){
            meFragment = new MeFragment();
        }
        return meFragment;
    }

    private Fragment getClassfyFragment(){
        if(classfyFragment == null){
            classfyFragment = new ClassfyFragment();
        }
        return classfyFragment;
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN&&keyCode == KeyEvent.KEYCODE_BACK){
            if((System.currentTimeMillis() - exitTime) > 2000){
                ToastUtil.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            }else{
                App.getInstance().exitApp();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
